// pages/register/register.js
import getCheckCode from "../../api/getCheckCode";
import userRegister from "../../api/userRegister";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        idNum: undefined,
        name: undefined,
        pwd: undefined,
        pwd2: undefined,
        phone: undefined,
        checkCode: undefined,
        countTimer:"获取验证码",
        disable:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    idNum(e){
        this.setData({
            idNum: e.detail.value
        });
    },
    name(e) {
        this.setData({
            name: e.detail.value
        });
    },
    pwd(e){
        this.setData({
            pwd: e.detail.value
        });
    },
    pwd2(e){
        this.setData({
            pwd2: e.detail.value
        });
    },
    phone(e){
        this.setData({
            phone: e.detail.value
        });
    },
    checkCode(e){
        this.setData({
            checkCode: e.detail.value
        });
    },
    getCheckCode(){
        if(!this.data.idNum||this.data.idNum.length<15){
            wx.showToast({title: '请输入正确身份证', icon: 'none'});
            return
        };
        if (!this.data.phone||this.data.phone.length<11){
            wx.showToast({title: '请输入正确手机号', icon: 'none'});
            return
        }
        wx.showLoading();
        new getCheckCode(this.data.idNum,require("../../entity/businessType").注册,this.data.phone).http(()=>{
            this.setData({
                disable:true
            });
            require("../../utils/countDownTimer")(60*1000,1000,(count)=>{
                this.setData({
                    countTimer:(count/1000+"s"),
                })
            },()=>{
                this.setData({
                    countTimer:"获取验证码",
                    disable:false
                })
            });
        });
    },
    register(){
        if(!this.data.idNum||this.data.idNum.length<15){
            wx.showToast({title: '请输入正确身份证', icon: 'none'});
            return;
        }
        if (!this.data.name){
            wx.showToast({title: '请输入姓名', icon: 'none'});
            return
        }
        if(!this.data.pwd||this.data.pwd.length<6){
            wx.showToast({title: '请输入不少于6位密码', icon: 'none'});
            return;
        }
        if(!this.data.pwd2||this.data.pwd2.length<6||this.data.pwd2!==this.data.pwd){
            wx.showToast({title: '两次密码不一致', icon: 'none'});
            return;
        }
        if (!this.data.phone||this.data.phone.length<11){
            wx.showToast({title: '请输入正确手机号', icon: 'none'});
            return
        }
        if(!this.data.checkCode){
            wx.showToast({title: '请输入验证码', icon: 'none'});
            return;
        }
        wx.showLoading();
        new userRegister(this.data.name,this.data.idNum,this.data.phone,this.data.pwd,this.data.checkCode)
            .http(()=>{
            wx.showToast({title: '注册成功', icon: 'none'});
            wx.navigateBack();
        });
    }
});